<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="公司" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="companyid" >
              <j-search-select-tag v-model="model.companyid" placeholder="请选择公司" dict="sys_depart where org_type='1',depart_name,id" ></j-search-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="仓库编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" placeholder="请输入仓库编号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="仓库名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入仓库名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="是否管理货位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="islocation">
              <a-checkbox v-model="model.islocation" placeholder="请输入仓库名称"></a-checkbox>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="仓库类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="whtype">
              <j-search-select-tag placeholder="请选择仓库类型" v-model="model.whtype" 
              :dictOptions="dictOptions">
              </j-search-select-tag>
              {{ selectValue }}
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="供应商" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="vendorid">
              <c-select-vendor v-model="model.vendorid" :buttons="false" placeholder="请选择供应商"></c-select-vendor>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="客户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customerid">
              <c-select-customer v-model="model.customerid" :buttons="false" placeholder="请选择客户"></c-select-customer>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="默认库存状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="defaultstocktypeid">
            <j-search-select-tag v-model="model.defaultstocktypeid" dict="scmstocktype,stockname,stockcode"></j-search-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="默认收货库存状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="defaultinstocktypeid">
            <j-search-select-tag v-model="model.defaultinstocktypeid" dict="scmstocktype,stockname,stockcode"></j-search-select-tag>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="可负库存" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="canfkc">
            <a-checkbox v-model="model.canfkc"></a-checkbox>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="参与MRP" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="canmrp">
            <a-checkbox v-model="model.canmrp"></a-checkbox>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="参与预警" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="canalter">
            <a-checkbox v-model="model.canalter"></a-checkbox>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'WarehouseForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        dictOptions:[
          {
            text:"普通仓库",
            value:'0'
          },
          {
            text:"供应商仓库",
            value:'1'
          },
          {
            text:"客户仓库",
            value:'2'
          },
          {
            text:"车间仓库",
            value:'3'
          },
          {
            text:"第三方仓库",
            value:'4'
          },
        ],
        model:{
         },
        labelCol: {
          xs: { span: 12 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 12 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           code: [
              { required: true, message: '请输入仓库编号!'},
           ],
           name: [
              { required: true, message: '请输入仓库名称!'},
           ],
        },
        url: {
          add: "/base/warehouse/add",
          edit: "/base/warehouse/edit",
          queryById: "/base/warehouse/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>